<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\Auth;
use app\admin\model\Role;
use think\facade\View;

class Base
{
    public function __construct()
    {
        $this->getnav();
    }

    //获取左侧菜单权限
    public function getnav()
    {
        //获取当前登录的管理员，左侧菜单显示的权限
        //获取当前管理员的信息（role_id）
        $name = session('name');
        $role = \app\admin\model\Admin::field('id,role_id')->where('username',$name)->find();
        //如果是超级管理员，直接查询权限表
        if ($role['role_id'] == 1) {
            //查询所有菜单权限
            $nav = Auth::where('is_nav', 1)->select();
        } else {
            //如果是普通管理员，先查询角色表 取到role_auth_ids
            $role = Role::find($role['role_id']);
            $role_auth_ids = explode('_',$role->role_auth_ids);
            //查询拥有的菜单权限
            $nav = Auth::where('is_nav',1)
                ->whereIn('id',$role_auth_ids)
                ->select();
        }
        $nav = fsFlat($nav);
        //变量赋值
        View::assign('nav',$nav);
        $controller = request()->controller();
        $action = request()->action();

        $auth = Auth::where(['auth_c'=>$controller, 'auth_a'=>$action])->find();

        $current_auth_ids = explode('_', $auth['pid_path']);
        $current_auth_ids[] = $auth['id'];
        View::assign('current_auth_ids', $current_auth_ids);
        $data = \app\admin\model\Admin::find($role['id']);
        View::assign('data',$data);
    }
}
